package logger

import (
	"fmt"
	"time"

	alibaba "github.com/aliyun/aliyun-log-go-sdk"

	"beesoft.ink/application/media/app/helper/helper"
)

func NewAlibaba(endpoint, accessKeyID, accessKeySecret, project, logstore string) *Alibaba {
	source := "go-sdk"
	topic := "example"

	client := &alibaba.Client{
		Endpoint:        endpoint,
		AccessKeyID:     accessKeyID,
		AccessKeySecret: accessKeySecret,
	}

	return &Alibaba{
		client:   client,
		project:  project,
		logstore: logstore,
		topic:    topic,
		source:   source,
	}
}

type Alibaba struct {
	client     *alibaba.Client
	project    string // 项目
	logstore   string // 存储
	topic      string // 主题 __topic__
	source     string // 源 __source__
	queryExp   string // 查询
	from       int64  // 开始时间戳
	to         int64  // 结束时间戳
	maxLineNum int64  // 最大数量
	offset     int64  // 偏移
	reverse    bool   // 逆序
}

// __tag__ __client_ip__ __receive_time__

func (r *Alibaba) Log(message string) error {
	logGroup := &alibaba.LogGroup{
		Topic:  helper.Ptr(r.topic),
		Source: helper.Ptr(r.source),
		Logs: []*alibaba.Log{
			{
				Time: helper.Ptr(uint32(time.Now().Unix())), // time
				Contents: []*alibaba.LogContent{
					{
						Key:   helper.Ptr("message"),
						Value: helper.Ptr(message),
					},
				},
			},
		},
	}

	err := r.client.PutLogs(r.project, r.logstore, logGroup)
	if err != nil {
		return fmt.Errorf("failed to logger to Alibaba: %v", err)
	}

	return nil
}
func (r *Alibaba) List(queryExp string) error {
	// result, err := r.client.GetLogs(r.project, r.logstore, r.topic, r.from, r.to, queryExp, r.maxLineNum, r.offset, r.reverse)
	// result, err := r.client.GetLogsByNano(r.project, r.logstore, r.topic, r.from, r.to, queryExp, r.maxLineNum, r.offset, r.reverse)
	result, err := r.client.GetLogsV2(r.project, r.logstore, &alibaba.GetLogRequest{
		Topic:   r.topic,
		Query:   r.queryExp,
		From:    time.Now().Unix() - 900,
		To:      time.Now().Unix(),
		Lines:   r.maxLineNum,
		Offset:  r.offset,
		Reverse: r.reverse,
		// PowerSQL      bool
		// FromNsPart    int32
		// ToNsPart      int32
		// NeedHighlight bool
		// IsAccurate    bool
	})
	// result2, err := r.client.GetLogsV3(r.project, r.logstore, &alibaba.GetLogRequest{
	// 	Topic:   r.topic,
	// 	Query:   r.queryExp,
	// 	From:    time.Now().Unix() - 900,
	// 	To:      time.Now().Unix(),
	// 	Lines:   r.maxLineNum,
	// 	Offset:  r.offset,
	// 	Reverse: r.reverse,
	// 	// PowerSQL      bool
	// 	// FromNsPart    int32
	// 	// ToNsPart      int32
	// 	// NeedHighlight bool
	// 	// IsAccurate    bool
	// })
	if err != nil {
		return fmt.Errorf("failed to query Alibaba: %v", err)
	}
	fmt.Println(result)

	return nil
}
func (r *Alibaba) ListLine(queryExp string) error {
	// result, err := r.client.GetLogLines(r.project, r.logstore, r.topic, r.from, r.to, queryExp, r.maxLineNum, r.offset, r.reverse)
	// result, err := r.client.GetLogLinesByNano(r.project, r.logstore, r.topic, r.from, r.to, queryExp, r.maxLineNum, r.offset, r.reverse)
	result, err := r.client.GetLogLinesV2(r.project, r.logstore, &alibaba.GetLogRequest{
		Topic:   r.topic,
		Query:   r.queryExp,
		From:    time.Now().Unix() - 900,
		To:      time.Now().Unix(),
		Lines:   r.maxLineNum,
		Offset:  r.offset,
		Reverse: r.reverse,
		// PowerSQL      bool
		// FromNsPart    int32
		// ToNsPart      int32
		// NeedHighlight bool
		// IsAccurate    bool
	})
	if err != nil {
		return fmt.Errorf("failed to query Alibaba: %v", err)
	}
	fmt.Println(result)

	return nil
}
func (r *Alibaba) ListToCompleted(queryExp string) error {
	// result, err := r.client.GetLogsToCompleted(r.project, r.logstore, r.topic, r.from, r.to, queryExp, r.maxLineNum, r.offset, r.reverse)
	// result, err := r.client.GetLogsToCompletedV2(r.project, r.logstore, &alibaba.GetLogRequest{
	// 	Topic:   r.topic,
	// 	Query:   r.queryExp,
	// 	From:    time.Now().Unix() - 900,
	// 	To:      time.Now().Unix(),
	// 	Lines:   r.maxLineNum,
	// 	Offset:  r.offset,
	// 	Reverse: r.reverse,
	// 	// PowerSQL      bool
	// 	// FromNsPart    int32
	// 	// ToNsPart      int32
	// 	// NeedHighlight bool
	// 	// IsAccurate    bool
	// })
	result, err := r.client.GetLogsToCompletedV3(r.project, r.logstore, &alibaba.GetLogRequest{
		Topic:   r.topic,
		Query:   r.queryExp,
		From:    time.Now().Unix() - 900,
		To:      time.Now().Unix(),
		Lines:   r.maxLineNum,
		Offset:  r.offset,
		Reverse: r.reverse,
		// PowerSQL      bool
		// FromNsPart    int32
		// ToNsPart      int32
		// NeedHighlight bool
		// IsAccurate    bool
	})
	if err != nil {
		return fmt.Errorf("failed to query Alibaba: %v", err)
	}
	fmt.Println(result)

	return nil
}
